package com.source.labelAndBarcode.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.source.labelAndBarcode.entity.BarcodeGrant;
import com.source.labelAndBarcode.service.BarcodeGrantService;
import com.source.utils.CommonResult;
import com.source.utils.ResultEnum;
import com.source.labelAndBarcode.vo.BarcodeGrantVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Joey
 * @since 2021-02-23
 */
@Api(tags = "条形码发放")
@RestController
@RequestMapping("/labelAndBarcode/barcode-grant")
public class BarcodeGrantController {

    @Autowired
    private BarcodeGrantService grantService;

    @ApiOperation(value = "条形码发放")
    @PostMapping("/barcodeGrant")
    public CommonResult<String> barcodeGrant(@RequestBody BarcodeGrant barcodeGrant){
        if(grantService.save(barcodeGrant)){
            return new CommonResult<String>(ResultEnum.SUCCESS);
        }
        return new CommonResult<String>(ResultEnum.ERROR);
    }

    @ApiOperation(value = "查看条形码发放")
    @GetMapping("/findBarcodeGrantAll")
    public CommonResult<List<BarcodeGrantVo>> findAll(){
        List<BarcodeGrantVo> list = grantService.findBarcodeGrantAll();
        return new CommonResult<List<BarcodeGrantVo>>(ResultEnum.SUCCESS,list);
    }

    @ApiOperation(value = "查看条形码发放", notes = "分页")
    @GetMapping("/findAllOnPagination")
    public CommonResult<IPage<BarcodeGrantVo>> findOnPagination(@RequestParam(value = "current", defaultValue = "1") Integer current,
                                                                @RequestParam(value = "size", defaultValue = "10") Integer size){

        Page<BarcodeGrantVo> page = new Page<BarcodeGrantVo>(current, size);
        IPage<BarcodeGrantVo> pages = grantService.findBarcodeGrantAllOnPagination(page);
        return new CommonResult<IPage<BarcodeGrantVo>>(ResultEnum.SUCCESS,pages);
    }

    @ApiOperation(value = "模糊查询条形码发放", notes = "分页")
    @GetMapping("/findByKeyWord")
    public CommonResult<IPage<BarcodeGrantVo>> findByKeyWord(@RequestParam(value = "keyWord", required = false) String keyWord,
                                                             @RequestParam(value = "current", defaultValue = "1") Integer current,
                                                             @RequestParam(value = "size", defaultValue = "10") Integer size){
        if(null == keyWord){
            return findOnPagination(current, size);
        }
        Page<BarcodeGrantVo> page = new Page<BarcodeGrantVo>(current, size);
        IPage<BarcodeGrantVo> pages = grantService.findByKeyWord(page, keyWord);
        return new CommonResult<IPage<BarcodeGrantVo>>(ResultEnum.SUCCESS,pages);
    }

    @ApiOperation(value = "回显条形码发放（用于明细）")
    @GetMapping("/findBarcodeGrantVoById/{id}")
    public CommonResult<BarcodeGrantVo> findVoById(@PathVariable String id){
        BarcodeGrantVo grant = grantService.findById(id);
        return new CommonResult<BarcodeGrantVo>(ResultEnum.SUCCESS,grant);
    }

    @ApiOperation(value = "回显条形码发放（用于修改条形码发放）")
    @GetMapping("/findBarcodeGrantById/{id}")
    public CommonResult<BarcodeGrant> findById(@PathVariable String id){
        BarcodeGrant grant = grantService.getById(id);
        return new CommonResult<BarcodeGrant>(ResultEnum.SUCCESS,grant);
    }

    @ApiOperation(value = "修改条形码发放")
    @PutMapping("/updateBarcodeGrant")
    public CommonResult<String> updateBarcodeGrant(@RequestBody BarcodeGrant grant){
        if(grantService.updateById(grant)){

            return new CommonResult<String>(ResultEnum.SUCCESS);
        }
        return new CommonResult<String>(ResultEnum.ERROR);
    }

    @ApiOperation(value = "删除条形码发放")
    @DeleteMapping("/deleteBarcodeGrant/{id}")
    public CommonResult<String> deleteBarcodeGrant(@PathVariable String id){
        if(grantService.removeById(id)){
            return new CommonResult<String>(ResultEnum.SUCCESS);
        }
        return new CommonResult<String>(ResultEnum.ERROR);
    }

    @ApiOperation(value = "批量删除条形码发放，id以英文逗号形式隔开")
    @DeleteMapping("/deleteBarcodeGrantByIds/{ids}")
    public CommonResult<String> deleteBarcodeGrantByIds(@PathVariable List<String> ids){
        try {
            if(grantService.removeByIds(ids)){
                return new CommonResult<String>(ResultEnum.SUCCESS);
            }
            return new CommonResult<String>(ResultEnum.ERROR, "操作失败");
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult<String>(ResultEnum.ERROR);
        }
    }

    @ApiOperation(value = "条形码对应时间段查询条形码数", notes = "当输入的是年、月、日，如2021-02-22，相当于 2021-02-21 23:59:59，相当于开区间")
    @GetMapping("/getGrantCountByTime")
    public CommonResult<Integer> getGrantCountByTime(@RequestParam("fromTime") Date fromTime,
                                                     @RequestParam("toTime") Date toTime){
        int count = grantService.getGrantCountByTime(fromTime, toTime);
        return new CommonResult<Integer>(ResultEnum.SUCCESS,count);
    }



}

